from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from extens import db
from app import create_app
from user_common.models import User
from feeds.models import Feed
import sys
import io
from utils import RedisOperation
from network.NetworkModel_pb2 import FeedItem

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.option('-a', '--account', dest='account')
@manager.option('-p', '--password', dest='password')
@manager.option('-n', '--name', dest='name')
def add_user(account, password, name):
    user = User.query.filter_by(account=account).first()
    if user is not None:
        print('账号为{}的用户已存在，无法添加'.format(account))
        return
    user = User(account=account, password=password, name=name)
    db.session.add(user)
    db.session.commit()
    print('添加用户成功')


@manager.option('-a', '--account', dest='account')
def remove_user(account):
    user = User.query.filter_by(account=account).first()
    if user is not None:
        db.session.delete(user)
        db.session.commit()
    print('删除用户成功')


@manager.command
def show_all_users():
    users = User.query.all()
    for user in users:
        print(user.id, user.account, user.name)


@manager.command
def remove_all_users():
    users = User.query.all()
    for user in users:
        db.session.delete(user)
    db.session.commit()
    print('删除所有用户成功')


@manager.option('-t', '--t', dest='title')
def add_feed(title):
    feed = Feed.query.filter_by(title=title).first()
    if feed is not None:
        print('title为{}的feed已存在，无法添加'.format(title))
        return
    feed = Feed(title=title)
    db.session.add(feed)
    db.session.commit()
    print('添加feed成功'.format(title))
    return


@manager.option('-t', '--title', dest='title')
def remove_feed(title):
    feed = Feed.query.filter_by(title=title).first()
    if feed is not None:
        db.session.delete(feed)
        db.session.commit()
    print('删除feed成功')


@manager.command
def add_all_feeds():
    arr = ['学习', '游戏', '户外', '体育', '桌游', '才艺', '颜值', '美食', '综合']
    for title in arr:
        feed = Feed.query.filter_by(title=title).first()
        if feed is None:
            feed = Feed(title=title)
            db.session.add(feed)
    db.session.commit()
    print('添加成功')
    show_all_feeds()


@manager.command
def remove_all_feeds():
    feeds = Feed.query.all()
    for feed in feeds:
        db.session.delete(feed)
    db.session.commit()
    print('删除成功')


@manager.command
def show_all_feeds():
    feeds = Feed.query.order_by(Feed.id).all()
    for f in feeds:
        print(f.id, f.title)


@manager.option('-f', '--fid', dest='fid')
@manager.option('-c', '--count', dest='count')
def add_live_room(fid, count):
    count = int(count)
    fid = int(fid)
    if count < 0:
        print('-c | --count 应该大于0')
        return
    if Feed.query.filter_by(id=fid).first() is None:
        print('fid 不存在')
        return
    rdm = RedisOperation(db=fid)
    for i in range(0, count):
        uid = str(i + 1)
        item = FeedItem()
        item.snapshotUrl = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555692792431&di=6883554a8f5a307882f6c3bde6c7b45b&imgtype=0&src=http%3A%2F%2Fsinastorage.com%2Fstorage.caitou.sina.com.cn%2Fproducts%2F201612%2F08c37ddca9b8cafefa9733b898bb33ef.jpeg'
        item.avatarUrl = 'http://tupian.qqjay.com/tou2/2018/0826/2670d6307e76f8c9458783c2f59f3083.jpg'
        item.name = 'Hins'
        item.roomTitle = '好好学习天天向上！'
        item.ruid = uid
        item.fid = str(fid)
        item = item.SerializeToString()

        rdm.set_data(uid, item, 60)


if __name__ == '__main__':
    manager.run()
